package io.renren.modules.live.service.impl;

import io.renren.common.utils.R;
import io.renren.modules.live.entity.FanweAnchorEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.live.dao.FanweAnchoridUseridDao;
import io.renren.modules.live.entity.FanweAnchoridUseridEntity;
import io.renren.modules.live.service.FanweAnchoridUseridService;

import javax.annotation.Resource;


@Service("fanweAnchoridUseridService")
public class FanweAnchoridUseridServiceImpl extends ServiceImpl<FanweAnchoridUseridDao, FanweAnchoridUseridEntity> implements FanweAnchoridUseridService {

    @Resource
    private FanweAnchoridUseridDao anchoridUseridDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FanweAnchoridUseridEntity> page = this.page(
                new Query<FanweAnchoridUseridEntity>().getPage(params),
                new QueryWrapper<FanweAnchoridUseridEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<FanweAnchorEntity> attentionAnchorInfo(Integer userId) {
        return anchoridUseridDao.attentionAnchorInfo(userId);
    }

    @Override
    public R addAttentionAnchorInfo(Integer userId, Integer anchorId) {
        //根据用户ID查询之前是否已经关注(已经关注 返回消息提示  ,没有关注就添加)
        List<Integer> anchorIdList = anchoridUseridDao.findByUserId(userId);
        if(anchorIdList.isEmpty()&&anchorIdList.size()==0){
            anchoridUseridDao.addAttentionAnchorInfo(userId,anchorId);
        }
        for (Integer temp : anchorIdList) {
            if(!temp.equals(anchorId)){
                anchoridUseridDao.addAttentionAnchorInfo(userId,anchorId);
            }else {
                return R.error("您已关注主播了");
            }
        }
        return R.ok("关注主播成功了");
    }

    @Override
    public void deleteaAttentionAnchorInfo(Integer userId, Integer anchorId) {
        anchoridUseridDao.deleteaAttentionAnchorInfo(userId,anchorId);
    }

}